﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Classes {
    import Engine.Managers.*;
    import flash.utils.*;

    public class IdleTaskQueue {

        private static const UPDATE_INTERVAL:Number = 1000;

        protected static var m_instance:IdleTaskQueue;

        private var m_Queue:Array;
        private var m_LastTaskExecuted:Number = 0;
        private var m_PopupQueueEmpty:Boolean = false;
        private var m_PopupQueueEmptySince:Number = 0;
        private var m_ActionQueueEmpty:Boolean = false;
        private var m_ActionQueueEmptySince:Number = 0;
        private var m_nextUpdateTime:Number = 0;
        private var m_queuePaused:Boolean = false;

        public function IdleTaskQueue(){
            this.m_Queue = new Array();
            super();
        }
        public static function getInstance():IdleTaskQueue{
            var _local1:Class;
            if (m_instance == null){
                _local1 = GlobalEngine.engineOptions.idleTaskQueue;
                m_instance = new (_local1)();
            };
            return (m_instance);
        }

        public function addTask(_arg1:BaseIdleTask):void{
            var _local3:String;
            var _local4:int;
            var _local5:BaseIdleTask;
            var _local2:Boolean = _arg1.shouldAdd;
            if (((_local2) && (_arg1.singleton))){
                _local3 = getQualifiedClassName(_arg1);
                _local4 = 0;
                while (_local4 < this.m_Queue.length) {
                    _local5 = this.m_Queue[_local4];
                    if (getQualifiedClassName(_local5) == _local3){
                        if (_arg1.singletonClobber){
                            this.m_Queue.splice(_local4, 1);
                            _local2 = true;
                        } else {
                            _local2 = false;
                        };
                        break;
                    };
                    _local4++;
                };
            };
            if (_local2){
                this.m_Queue.push(_arg1);
            };
        }
        public function checkTasks():void{
            var _local1:BaseIdleTask;
            var _local2:int;
            var _local3:BaseIdleTask;
            if ((((GlobalEngine.currentTime > this.m_nextUpdateTime)) && (!(this.m_queuePaused)))){
                this.m_nextUpdateTime = (GlobalEngine.currentTime + IdleTaskQueue.UPDATE_INTERVAL);
                this.checkTimeStamps();
                if (this.m_Queue.length > 0){
                    _local2 = 0;
                    while (_local2 < this.m_Queue.length) {
                        _local3 = this.m_Queue[_local2];
                        if (this.checkIfTaskCanRun(_local3)){
                            _local1 = _local3;
                            this.m_Queue.splice(_local2, 1);
                            break;
                        };
                        _local2++;
                    };
                    if (_local1 != null){
                        this.m_LastTaskExecuted = GlobalEngine.currentTime;
                        _local1.fire();
                    };
                };
            };
        }
        private function checkIfTaskCanRun(_arg1:BaseIdleTask):Boolean{
            var _local2:Number = GlobalEngine.currentTime;
            var _local3:Boolean;
            if (_arg1.requireActionQueueIdle){
                if (((!(this.m_ActionQueueEmpty)) || (((_local2 - this.m_ActionQueueEmptySince) < _arg1.minActionIdleMilliseconds)))){
                    _local3 = false;
                };
            };
            if (((_local3) && (_arg1.requireMouseIdle))){
                if ((_local2 - GlobalEngine.lastInputTick) < _arg1.minMouseIdleMilliseconds){
                    _local3 = false;
                };
            };
            if (((_local3) && (_arg1.requirePopupQueueEmpty))){
                if (((!(this.m_PopupQueueEmpty)) || (((_local2 - this.m_PopupQueueEmptySince) < _arg1.minPopupIdleMilliseconds)))){
                    _local3 = false;
                };
            };
            if (((_local3) && (((_local2 - this.m_LastTaskExecuted) < _arg1.queueCoolDownMilliseconds)))){
                _local3 = false;
            };
            if (((_local3) && (!(_arg1.canFire)))){
                _local3 = false;
            };
            return (_local3);
        }
        private function checkTimeStamps():void{
            if (PopupQueueManager.getInstance().hasActiveDialog() == false){
                if (!this.m_PopupQueueEmpty){
                    this.m_PopupQueueEmptySince = GlobalEngine.currentTime;
                };
                this.m_PopupQueueEmpty = true;
            } else {
                this.m_PopupQueueEmpty = false;
            };
            if (this.isQueuedActionsEmpty()){
                if (!this.m_ActionQueueEmpty){
                    this.m_ActionQueueEmptySince = GlobalEngine.currentTime;
                };
                this.m_ActionQueueEmpty = true;
            } else {
                this.m_ActionQueueEmpty = false;
            };
        }
        protected function isQueuedActionsEmpty():Boolean{
            return (true);
        }
        public function togglePause(_arg1:Boolean):void{
            this.m_queuePaused = _arg1;
        }
        public function clearQueue():void{
            this.m_Queue = new Array();
        }

    }
}//package Engine.Classes 
